package psnl.bingo.bgtool.framework.config;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import psnl.bingo.bgtool.framework.interceptor.UserInfoInterceptor;
import psnl.bingo.bgtool.framework.listener.MyListener;

@Configuration
public class MyMVCConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 请求项目的以下URL：/、/login、/index、/index.html都会访问默认页面
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login").setViewName("login");
        registry.addViewController("/index").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");

        registry.addViewController("/list.html").setViewName("list");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInfoInterceptor())
                .addPathPatterns("/rename/*", "/config/*");
    }

    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean();
        listener.setEnabled(true);
        listener.setListener(new MyListener());
        listener.setOrder(1);
        return listener;
    }

}
